package org.jeecg.modules.iot.middleware.processor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.jt808.JTT808Message;
import org.jeecg.modules.iot.middleware.util.AddressUtil;
import org.jeecg.modules.iot.model.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.modules.iot.model.UniqueSerialNumberGenerator;
import org.jeecg.modules.iot.model.modbus.ModbusParams;
import org.jeecg.modules.iot.middleware.util.JsonUtil;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public    class JT808Processor {

    private final JT808MessageGeneratorFactory messageGeneratorFactory;
    @Autowired
    public JT808Processor(JT808MessageGeneratorFactory messageGeneratorFactory) {
        this.messageGeneratorFactory = messageGeneratorFactory;
    }
   /* @Autowired
    MqttSendClient mqttSendClient;
*/
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AddressUtil addressUtil;

    public Integer process(RentalEBike car, MessageType messageType, Jt808CentralControlConfig config) {
        Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
        try {
        ObjectMapper objectMapper = new ObjectMapper();
        String sim = car.getPhone();

        // 生成编码信息
        JTT808Message message = new JTT808Message();
        message.setSim(sim);
        message.setSequence(seq);
        JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType.name());
        String encodedMessage = generator.generateMessage(message,config);

        //JT808MessageGenerator generator = getMessageGenerator();
        //String encodedMessage = generator.generateMessage(message);
        // 发送消息到mqtt
        String sendJson = JsonUtil.createMqttMessageJson(encodedMessage, car, messageType.getType(), String.valueOf(seq));

  /*      // 这里应该有发送MQTT消息的逻辑
        mqttSendClient.publish(true, car.getCarCode(),sendJson);
*/
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return seq;
    }
    public Integer process(RentalEBike car,MessageType messageType) {
        Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
        try {
        ObjectMapper objectMapper = new ObjectMapper();
        String sim = car.getPhone();


        // 生成编码信息
        JTT808Message message = new JTT808Message();
        message.setSim(sim);
        message.setSequence(seq);
        JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType.name());
        String encodedMessage = generator.generateMessage(message);

        //JT808MessageGenerator generator = getMessageGenerator();
        //String encodedMessage = generator.generateMessage(message);
        // 发送消息到mqtt
        String sendJson = JsonUtil.createMqttMessageJson(encodedMessage, car, messageType.getType(), String.valueOf(seq));

     /*   // 这里应该有发送MQTT消息的逻辑
        mqttSendClient.publish(true, car.getCarCode(),sendJson);
*/
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return seq;
    }
    public Integer processTransparent(RentalEBike car, MessageType messageType, ModbusParams modbusParams) {
        Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
        ;
        String hexString = String.format("%04x", seq); // 使用 %x 输出小写十六进制
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String sim = car.getPhone();


            // 生成编码信息
            JTT808Message message = new JTT808Message();
            message.setSim(sim);
            message.setSequence(seq);
            JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType.name());
            String encodedMessage = generator.generateMessage(message,modbusParams);


            // 发送消息到mqtt
            String sendJson  = JsonUtil.createMqttMessageJson(encodedMessage, car, messageType.getType(), hexString);
            addressUtil.addAddressMapping(car.getCarCode(), hexString,modbusParams.getStartAddress(),100);
          /*  // 这里应该有发送MQTT消息的逻辑
            mqttSendClient.publish(true, car.getCarCode(),sendJson);
*/
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return seq;
    }


    public String processTransparentMqttJson(RentalEBike car, MessageType messageType, ModbusParams modbusParams) {
         Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
         String hexString = String.format("%04x", seq); // 使用 %x 输出小写十六进制

       /* Integer seq=0x15;
        String hexString = "0015";*/
        String sendJson ="";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String sim = car.getPhone();


            // 生成编码信息
            JTT808Message message = new JTT808Message();
            message.setSim(sim);
            message.setSequence(seq);
            JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType.name());
            String encodedMessage = generator.generateMessage(message,modbusParams);


            // 发送消息到mqtt
            sendJson = JsonUtil.createMqttMessageJson(encodedMessage, car, messageType.getType(), hexString);

             //存入流水号记录
            addressUtil.addAddressMapping(car.getCarCode(), hexString,modbusParams.getStartAddress(),100);
           /* // 这里应该有发送MQTT消息的逻辑
            mqttSendClient.publish(true, car.getCarCode(),sendJson);
*/
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sendJson;
    }





}
